package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Contend;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.ContendService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping({"contend"})
public class ContendConntroller extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.ContendConntroller.class);

    @Resource
    private ContendService contendService;

    @Resource
    private CompanyService companyService;

    @GetMapping({"contendlist"})
    public JsonResult<List<Contend>> getlistByCompany_id() {
        List<Contend> list = this.contendService.getContendList();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @GetMapping({"contend"})
    public JsonResult<Object> contend(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String company_name) {
        PageResult pageResult = this.contendService.getListpage(pageNum, pageSize, company_name);
        log.info("contend---------------------------------->pageNum:" + pageNum);
        log.info("contend---------------------------------->pageSize:" + pageSize);
        log.info("contend---------------------------------->company_name:" + company_name);
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addcontend"}, method = {RequestMethod.POST})
    public JsonResult<Void> addcontend(Contend contend) {
        this.contendService.addcontend(contend);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"updatecontend"}, method = {RequestMethod.POST})
    public JsonResult<Void> updatecontend(Contend contend) {
        this.contendService.updatecontend(contend);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"deletecontend/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deletecontend(@PathVariable("id") int id) {
        this.contendService.deletecontend(id);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findcontendByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Contend> findcontendByid(@PathVariable("id") int id) {
        Contend contend = this.contendService.findcontendByid(id);
        if (contend == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", contend);
    }

    @RequestMapping(path = {"showcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> showcompany() {
        List<Company> list = this.companyService.getCommpanylist();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }
}
